# Copyright 2019 The IREE Authors
#
# Licensed under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

# Tests for end-to-end IREE support of specific features to prevent regression.
# These should focus on support by IREE itself, not for issues with specific runner tools.
# Place those tests in tools/test/

load("//build_tools/bazel:iree_check_test.bzl", "iree_check_single_backend_test_suite")
load("//build_tools/bazel:iree_lit_test.bzl", "iree_lit_test_suite")

package(
    features = ["layering_check"],
    licenses = ["notice"],  # Apache 2.0
)

BACKEND_TESTS = [
    "i1_inlined_constant.mlir",
    "linalg_ops.mlir",
    "reduction_broadcast_elementwise.mlir",
    "scalar_computation.mlir",
    "softmax.mlir",
    "strided_slice.mlir",
    "transpose.mlir",
]

iree_lit_test_suite(
    name = "lit",
    srcs = [
        "fill_i64.mlir",
        "force_single_dispatch.mlir",
        "globals.mlir",
        "libm_linking.mlir",
        "scalar.mlir",
        "trace_dispatch_tensors.mlir",
        "unused_args.mlir",
    ],
    cfg = "//tests:lit.cfg.py",
    tags = [
        "driver=local-task",
        "hostonly",
    ],
    tools = [
        "//tools:iree-opt",
        "//tools:iree-run-mlir",
        "@llvm-project//lld",
        "@llvm-project//llvm:FileCheck",
    ],
)

iree_check_single_backend_test_suite(
    name = "check_fp_reassoc_regression_llvm-cpu",
    srcs = [
        "associative_reordering.mlir",
    ],
    compiler_flags = [
        "--iree-llvmcpu-reassociate-fp-reductions=false",
        "--iree-llvmcpu-target-cpu=generic",
    ],
    driver = "local-task",
    target_backend = "llvm-cpu",
)

iree_check_single_backend_test_suite(
    name = "check_regression_llvm-cpu",
    srcs = [
        "layernorm.mlir",
        "pack_pad_transpose_1x9_into_2x1x8x4_issue_12546.mlir",
        "split_reduction_using_tiling.mlir",
    ] + BACKEND_TESTS,
    compiler_flags = ["--iree-llvmcpu-target-cpu=generic"],
    driver = "local-task",
    target_backend = "llvm-cpu",
)

# TODO(22013): Merge the test suite into the main one, after the issue is fixed.
iree_check_single_backend_test_suite(
    name = "check_regression_dynamic_gather_attention_llvm-cpu",
    srcs = [
        "dynamic_gather_attention.mlir",
    ],
    compiler_flags = ["--iree-llvmcpu-target-cpu=generic"],
    driver = "local-task",
    tags = [
        "noriscv",
    ],
    target_backend = "llvm-cpu",
)

iree_check_single_backend_test_suite(
    name = "check_regression_tosa_llvm-cpu",
    srcs = [
        # --iree-input-type=tosa is interesting for this test because
        # it includes the --iree-linalg-quantized-matmul-to-matmul pass,
        # so this tests provides e2e coverage for that pass.
        "linalg_quantized_matmul_vs_linalg_matmul.mlir",
    ],
    compiler_flags = ["--iree-llvmcpu-target-cpu=generic"],
    driver = "local-task",
    target_backend = "llvm-cpu",
)

iree_check_single_backend_test_suite(
    name = "check_regression_vmvx",
    srcs = [
        "layernorm.mlir",
    ] + BACKEND_TESTS,
    driver = "local-task",
    target_backend = "vmvx",
)

iree_check_single_backend_test_suite(
    name = "check_regression_vulkan-spirv",
    # TODO(#10024): Fix layernorm.mlir on Pixel 6 and put in BACKEND_TESTS.
    srcs = BACKEND_TESTS,
    driver = "vulkan",
    target_backend = "vulkan-spirv",
)

iree_check_single_backend_test_suite(
    name = "check_regression_cuda",
    srcs = [
        "large_reduction.mlir",
        "layernorm.mlir",
    ] + BACKEND_TESTS,
    driver = "cuda",
    tags = [
        # CUDA cuInit fails with sanitizer on.
        "noasan",
        "nomsan",
        "notsan",
        "noubsan",
        "requires-gpu-nvidia",
    ],
    target_backend = "cuda",
)

iree_check_single_backend_test_suite(
    name = "check_regression_hip",
    srcs = [
        "dynamic_gather_attention.mlir",
        "linalg_ops_dynamic.mlir",
        "split_reduction_using_tiling.mlir",
    ],
    compiler_flags = [
        "--iree-opt-level=O3",
    ],
    driver = "hip",
    tags = [
        "noasan",
        "nomsan",
        "notsan",
        "noubsan",
        "requires-gpu-amd",
    ],
    target_backend = "rocm",
)

iree_check_single_backend_test_suite(
    name = "aggressive_fusion_test_cuda",
    srcs = [
        "softmax.mlir",
        "softmax_large.mlir",
    ],
    compiler_flags = [
        "--iree-dispatch-creation-fuse-multi-use",
    ],
    driver = "cuda",
    tags = [
        # CUDA cuInit fails with sanitizer on.
        "noasan",
        "nomsan",
        "notsan",
        "noubsan",
        "requires-gpu-nvidia",
    ],
    target_backend = "cuda",
)

iree_check_single_backend_test_suite(
    name = "aggressive_fusion_test",
    srcs = [
        "softmax.mlir",
    ],
    compiler_flags = [
        "--iree-dispatch-creation-fuse-multi-use",
        "--iree-llvmcpu-target-cpu=generic",
    ],
    driver = "local-task",
    target_backend = "llvm-cpu",
)

# Compilation-only (no driver specified, the default None means compile only)
# regression testcases for VMVX backend with microkernels. Indeed we have had
# a couple of hard-to-diagnose issues that were purely compilation issue, no
# particular concern about correctness once compilation succeeds, and having
# compilation-only tests allows us to import the original unmodified testcase.
iree_check_single_backend_test_suite(
    name = "check_regression_compilation_only_vmvx_ukernels",
    srcs = [
        "dynamic_matmuls_on_same_accumulator_issue_12060.mlir",
    ],
    compiler_flags = [
        "--iree-vmvx-enable-microkernels",
    ],
    target_backend = "vmvx",
)
